<?php

	namespace Phptricks\system\library\PhptricksExceptions;

error_reporting(E_ALL);
//	error_reporting(0);
	set_error_handler('Phptricks\system\library\PhptricksExceptions\PhptricksExceptions::error_handlers');
	set_exception_handler(array('Phptricks\system\library\PhptricksExceptions\PhptricksExceptions', 'Exceptions'));

	/*
	 *  phptricks Framework 
	 *  link http://phptricks.net/Framework
	 *  date start 2014
	 */

	class PhptricksExceptions extends \Exception{

		public static $errors = array();

		public function __construct($message = '', $code = 0, \Exception $previous = null){
			parent::__construct($message, $code, $previous);
			if (empty($message) || !isset($message)) {
				return true;
			}
			if ($code == 8 || $code == 2) {
//				$get_array_trace = $this->getTrace();
//				pr($get_array_trace,1);
				self::$errors[$message] = array(
					'file' => $this->file,
					'line' => $this->line
				);
			} else {
				$this->PrintMessage($this->FormatMessage($message));
				return die();
			}
		}

		public static function Exceptions($message, $code = 0, \Exception $previous = null){
			$this->PrintMessage($this->FormatMessage($message));
			return die();
		}

		public static function error_handlers($code = 0, $error, $file, $line){
			if ($code == 8 || $code == 2) {
				self::$errors[$error] = array(
					'file' => $file,
					'line' => $line
				);
			} else {
				$PhptricksExceptions = new \Phptricks\system\library\PhptricksExceptions\PhptricksExceptions();
				$return_message = $PhptricksExceptions->FormatMessage($error, $file, $line, $code);
				echo $return_message;
			}
		}

		private function FormatMessage($message, $file = '', $line = '', $code = 0){
			$get_array_trace = $this->getTrace();
//			pr($get_array_trace);
			if (count($get_array_trace) > 0 && empty($file)) {
				$array_trace = $get_array_trace[0];
			} else {
				$array_trace = array(
					'line' => $this->line,
					'file' => $this->file,
					'class' => '',
					'function' => '',
					'args' => ''
				);
			}

			if (!empty($file)) {
				$array_trace['file'] = $file;
			}

			if (!empty($line)) {
				$array_trace['line'] = $line;
			}



			$return_message = "<pre><strong>Error:" . "\n&nbsp;&nbsp;" . 'Message:</strong>  ' . $message;
			if (!empty($array_trace['file'])) {
				$return_message .= "\n&nbsp;&nbsp;<strong>File:\t</strong>  " . $array_trace['file'] . ": <strong>at line ( " . $array_trace['line'] . " ) </strong>";
			}
			if (!empty($array_trace['line'])) {
				$return_message .= "\n&nbsp;&nbsp;<strong>Line:\t</strong>  <strong>" . $array_trace['line'] . "</strong>";
			}
			if (!empty($array_trace['class'])) {
				$return_message .= "\n&nbsp;&nbsp;<strong>Class:\t</strong>  " . $array_trace['class'];
			}
			if (!empty($array_trace['function'])) {
				$return_message .= "\n&nbsp;&nbsp;<strong>Function:</strong> " . $array_trace['function'];
			}
			if (!empty($array_trace['args'])) {
				$return_message .= "\n&nbsp;&nbsp;<strong>Args:</strong> " . self::GetArgs($array_trace['args']);
			}
			$return_message .= "\n&nbsp;&nbsp;<strong>Trace:</strong> \n" . $this->getTraceAsString();


			$return_message = '<style>
					.alert-danger {
						background-color: #f2dede;
						border-color: #ebccd1;
						color: #a94442;
						text-shadow: 0px 0px 1px #FFF;
					}
					.alert {
						display: block;
						clear: both;
						padding: 15px;
						border-radius: 0px;
						z-index: 9999999;
						position: fixed;
						min-height: 370px;
						max-height: 370px;
						bottom: -370px;
						opacity: 0.6;
						transition: ease 0.2s;
						-moz-transition: ease 0.3s;
						-o-transition: ease 0.3s;
						-webkit-transition: ease 0.3s;
						right:0px;
						left:0px;
					}
					.alert:hover{
						opacity: 1;
						bottom: 0px;
					}
					.alert pre{
						text-align: left;
						direction: ltr;
						min-height: 350px;
						max-height: 350px;
						overflow-y: scroll;
						margin: 0px;
						word-wrap: break-word;
					}
					.alert hr{
						border-top-color: #ADADAD;
						border-top-style: dashed;
						border-top-width: 1px;
						margin:5px 0px;
					}
					.alert strong{
						display:inline-block;
					}
					.alert h2{
						padding: 0px;
						margin: 0px;
						text-align: center;
						text-shadow: 1px 1px 0px #FFF;
						color: #D30808;
						position: relative;
						top: -9px;
						border-bottom: 1px #ADADAD dashed;
					}
				</style>
				<div class="alert alert-danger"><h2>Error</h2>' . $return_message . ' %s</div>';
			if (count(self::$errors) > 0) {
				$return_message = \sprintf($return_message, "<br><br><hr><br><strong>Other Mistakes:</strong><br>" . implode("<hr>", $this->error_list($print = true)));
			}
			return $return_message;
		}

		private static function GetArgs($args = array()){
			$retrun_string = array();
			foreach ($args as $key => $val) {
				if (is_array($val) || \is_object($val)) {
					$retrun_string[] = 'Array(' . self::GetArgs($val) . ' )';
				} else {
					$retrun_string[] = "'$val'";
				}
			}
			$retrun_string = rtrim(implode(' , ', $retrun_string), ' , ');
			return $retrun_string;
		}

		public function PrintMessage($message){
			echo "<pre>";
			echo $message;
			echo "</pre>";
		}

		public function error_list($print = true){
			if (count(self::$errors) > 0) {
				$error = self::$errors;
				$return_array = array();
				foreach ($error as $key => $val) {
					$return_message = "<strong>&nbsp;&nbsp;Message: </strong>" . $key;
					if (!empty($val['file'])) {
						$return_message .= "\n&nbsp;&nbsp;<strong>File: </strong>" . $val['file'] . ": <strong>at line ( " . $val['line'] . " ) </strong>";
					}
					$return_array[] = $return_message;
				}

				if ($print == true) {
					return $return_array;
				}


				if (count($return_array) > 0) {
					$message = \implode("<hr>", $return_array);
					$return_message = '<style>
					.alert-danger {
						background-color: #f2dede;
						border-color: #ebccd1;
						color: #a94442;
						text-shadow: 0px 0px 1px #FFF;
					}
					.alert {
						display: block;
						clear: both;
						padding: 15px;
						border: 1px solid transparent;
						border-radius: 0px;
						z-index: 9999999;
						position: fixed;
						width: 100%;
						min-height: 250px;
						max-height: 250px;
						bottom: -240px;
						opacity: 0.5;
						transition: ease 0.2s;
						-moz-transition: ease 0.3s;
						-o-transition: ease 0.3s;
						-webkit-transition: ease 0.3s;
					}
					.alert:hover{
						opacity: 1;
						bottom: -20px;
					}
					.alert pre{
						text-align: left;
						direction: ltr;
						min-height: 220px;
						max-height: 220px;
						overflow-y: scroll;
						word-wrap: break-word;
					}
					.alert hr{
						border-top-color: #ADADAD;
						border-top-style: dashed;
						border-top-width: 1px;
						margin:5px 0px;
					}
					.alert strong{
						display:inline-block;
					}
					.alert2{position: static !important; opacity: 1;}
				</style>
				<div class="alert alert-danger alert2">' . "<pre><strong>Error:</strong>\n" . $message . '</pre></div>';
					echo $return_message;
				}
			}
		}

	}
	